Namespace Views
  Public Class TreeNodeExpandedState

    Private mTaskID As Integer
    Private mChildList As IList(Of TreeNodeExpandedState) = New List(Of TreeNodeExpandedState)
    Private mExpanded As Boolean

    Sub New()
      'this is a virtual root node, use a fake task id
      mTaskID = -1
    End Sub

    Sub New(ByVal node As TreeNode)
      mExpanded = node.IsExpanded
      mTaskID = CInt(node.Tag)

      For Each lChildNode As TreeNode In node.Nodes
        mChildList.Add(New TreeNodeExpandedState(lChildNode))
      Next
    End Sub

    Public ReadOnly Property ChildList() As IList(Of TreeNodeExpandedState)
      Get
        Return mChildList
      End Get
    End Property

    Public ReadOnly Property Expanded() As Boolean
      Get
        Return mExpanded
      End Get
    End Property

    Public ReadOnly Property TaskId() As Integer
      Get
        Return mTaskID
      End Get
    End Property

    ''' <summary>
    ''' Return whether a given task is expanded or not.
    ''' The query is distributed among the children
    ''' </summary>
    ''' <param name="taskId">task id to look for</param>
    ''' <returns>true, which means it is; or false, which is default, 
    ''' and thus either means "i don't know" or "no"</returns>
    Public Function IsExpanded(ByVal taskId As Integer) As Boolean
      If mTaskID = taskId Then
        '-i know-
        Return Expanded
      End If
      'look in children
      Dim lIsExpanded As Boolean = False
      For Each lChild As TreeNodeExpandedState In ChildList
        lIsExpanded = lChild.IsExpanded(taskId)
        If lIsExpanded Then
          '-child knows-
          Return lIsExpanded
        End If
        '-child doesn't know or said no-
      Next

      '-i don't know-
      Return False
    End Function
  End Class
End Namespace

